package chaoyue.study.thread;

/**
 * 停止线程
 * 一般使用 interrupt 作为flag来自行判断当前线程是否需要退出
 */
public class ThreadStop2 {
    public static void main(String[] args) throws InterruptedException {
        Thread t1 = new Thread(() -> {
            System.out.println("线程开始...");
            while (true) {
                if (Thread.currentThread().isInterrupted()) {
                    System.out.println("线程退出...");
                    return;
                }
            }
        });
        t1.start();
        Thread.sleep(3000);
        t1.interrupt();
    }
}
